/* Copyright 2009. All rights reserved. */
package org.bling;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bling.parser.BlingParserException;
import org.bling.process.BlingProcessingException;
import org.bling.process.Request;
import org.bling.process.RequestProcessor;
import org.bling.process.context.ApplicationContext;
import org.bling.process.render.BlingRenderException;
import org.bling.util.Constants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

/**
 * 
 * All controllers in the Bling framework extend from this type
 * 
 * @author DISBROB
 *
 */
public abstract class ApplicationController {

	public Map application = ApplicationContext.getInstance().getContext();
	public Map params = new HashMap();
	public Map request = new HashMap();
	
	public String fileOpenDialog(String title, String filterPath, List<String> filterExtensions) {
		return fileDialog(SWT.OPEN, title, filterPath, "", filterExtensions);
	}

	public String fileSaveDialog(String title, String filterPath, String fileName, List<String> filterExtensions) {
		return fileDialog(SWT.SAVE, title, filterPath, fileName, filterExtensions);
	}
	
	private String fileDialog(int type, String title, String filterPath, String fileName, List<String> filterExtensions) {
		Display display = (Display) ApplicationContext.getInstance()
		.getAttribute(Constants.ApplicationContextAttributes.DISPLAY);
		Shell shell = display.getActiveShell();
		
		FileDialog fd = new FileDialog(shell, type);
		fd.setText(title);
		fd.setFilterPath(filterPath);
		fd.setFilterExtensions(filterExtensions.toArray(new String[0]));
		fd.setFileName(fileName);
		return fd.open();
	}
	
	public void infoOkDialog(String title, String message) {
		Display display = (Display) ApplicationContext.getInstance()
		.getAttribute(Constants.ApplicationContextAttributes.DISPLAY);
		Shell shell = display.getActiveShell();
		
		MessageBox messageBox = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK);
		messageBox.setText(title);
		messageBox.setMessage(message);
		messageBox.open();
	}

	public void errorOkDialog(String title, String message) {
		Display display = (Display) ApplicationContext.getInstance()
		.getAttribute(Constants.ApplicationContextAttributes.DISPLAY);
		Shell shell = display.getActiveShell();
		
		MessageBox messageBox = new MessageBox(shell, SWT.ICON_ERROR | SWT.OK);
		messageBox.setText(title);
		messageBox.setMessage(message);
		messageBox.open();
	}

	public void warningOkDialog(String title, String message) {
		Display display = (Display) ApplicationContext.getInstance()
		.getAttribute(Constants.ApplicationContextAttributes.DISPLAY);
		Shell shell = display.getActiveShell();
		
		MessageBox messageBox = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK);
		messageBox.setText(title);
		messageBox.setMessage(message);
		messageBox.open();
	}

	
	public void dialog(String path) {
		ApplicationContext appContext = ApplicationContext.getInstance();
		RequestProcessor requestProcessor = (RequestProcessor) appContext
				.getAttribute(Constants.ApplicationContextAttributes.REQUEST_PROCESSOR);
		
		try {
			Request request = new Request(path, true);
			requestProcessor.process(request);
		} catch (BlingProcessingException e) {
			// FIXME: Add some error handling
			throw new RuntimeException(e.getMessage());
		} catch (BlingParserException e) {
			// FIXME: Add some error handling
			throw new RuntimeException(e.getMessage());
		} catch (BlingRenderException e) {
			// FIXME: Add some error handling
			throw new RuntimeException(e.getMessage());
		}

	}
	
	public void closeWindow(String path) {
		ApplicationContext appContext = ApplicationContext.getInstance();
		RequestProcessor requestProcessor = (RequestProcessor) appContext
				.getAttribute(Constants.ApplicationContextAttributes.REQUEST_PROCESSOR);
		
	    requestProcessor.getRenderer().closeWindow(path);
    		
	}
}